using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Portal.User.Security;
using System.Web.Caching;
using Xmobile.Users;

namespace ChannelVN.CoreBO
{
    public class GetXuatBanBaibyCat
    {
        public static DataTable GetXuatBanBaiByCat(int Role_ID, string User_ID)
        {
            string cacheName = "GetXuatBanBaiByCat_" + Role_ID + "_" + User_ID;
            DataTable dt = (DataTable)HttpContext.Current.Cache.Get(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoreProcedure.Get_VIEW_PermissionWithCategory_by_UserID_and_RoleID(Role_ID, User_ID);
                    db.Close();
                    HttpContext.Current.Cache.Insert(cacheName, dt, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration);
                }
            }

            return dt;
        }

        public static string GetXuatBanBaiByCatArr(int Role_ID, string User_ID)
        {            
            string arr = "";
            string value = "";
            DataTable dt = new DataTable();           
            
            dt = GetXuatBanBaiByCat(Role_ID, User_ID);

            if (dt != null && dt.Rows.Count > 0)
            {
                string Cat_ID;
                int Permission;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Cat_ID = dt.Rows[i]["Cat_ID"].ToString();
                    Permission = Convert.ToInt32(dt.Rows[i]["Permission_ID"]);
                    if (Permission == PermissionConst.Xuat_Ban_Bai)
                    {
                        arr += "\"" + Cat_ID + "\",";
                    }
                }              

                value = arr;
            }            

            return value;
        }

        public static string GetAllCategoryByChannelId(string channelID)
        {
            string arr = "";
            string value = "";
            DataTable dt = new DataTable();

            using (MainDB db = new MainDB())
            {
                dt = db.StoreProcedure.GetAllCategoryByChannelId(channelID);
                db.Close();
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                string Cat_ID;                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Cat_ID =dt.Rows[i]["Cat_ID"].ToString();
                    arr += "\"" + Cat_ID + "\",";
                }

                value = arr;
            }

            return value;
        }

        public static DataTable Get_RoleID_by_UserID(string User_ID)
        {
            string cacheName = "Get_RoleID_by_UserID_" + User_ID;
            DataTable dt = (DataTable)HttpContext.Current.Cache.Get(cacheName);
            if (dt == null)
            {
                using (MainDB db = new MainDB())
                {
                    dt = db.StoreProcedure.Get_RoleID_by_UserID(User_ID);
                    db.Close();
                }

                HttpContext.Current.Cache.Insert(cacheName, dt, null, DateTime.Now.AddHours(23), Cache.NoSlidingExpiration);
            }

            return dt;
        }
    }
}
